// pages/voteCountdown/voteCountdown.js
import { $wuxCountDown } from '../../components/wux/index.js'
import activityApi from '../../service/activityApi'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day: '00',
    hour: '00',
    minute: '00',
    second: '00',
    emptyParams: {
      loadText: '加载中...',
      loadingFlag: true
    },
    // timeStamp: '1557244800000' // 2019.05.08 00.00.00
    timeStamp: '',
    matchStatus: '',
    matchStatusEnum: app.globalData.codeConfig.matchStatus,
    showCountdown: false,
    teamList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options.timeStamp) {
      this.setData({
        timeStamp: options.timeStamp
      })
    }
    if (options.matchStatus) {
      that.setData({
        matchStatus: options.matchStatus
      })
      if ((that.data.matchStatus - 0) === that.data.matchStatusEnum.REMATCH_LIST_PUBLIC) {
        that.initTimer(that.data.timeStamp - 0)
      }
    }
    this.getTeamList()
  },

  initTimer (timeStamp) {
    this.countDownTimer = new $wuxCountDown({
      date: (timeStamp - 0),
      render (date) {
        this.setData({
          day: this.leadingZeros(date.days, 2),
          hour: this.leadingZeros(date.hours, 2),
          minute: this.leadingZeros(date.min, 2),
          second: this.leadingZeros(date.sec, 2)
        })
      },
      onEnd () {
        wx.redirectTo({
          url: '/pages/vote/vote'
        })
      }
    })
  },
  // 查询战队列表
  getTeamList () {
    let that = this
    that.setEmptyStatus('loading')
    activityApi.getTeamList().then((res) => {
      wx.stopPullDownRefresh()
      if (app.globalData.codeConfig.errCode.SUCCESS_CODE === res.data.errCode) {
        that.setData({
          teamList: [...res.data.data.teamList]
        })
        if (that.data.teamList.length === 0) {
          that.setEmptyStatus('no-data')
        }
      } else {
        that.setEmptyStatus('no-data')
      }
    }).catch((res) => {
      wx.stopPullDownRefresh()
      that.setEmptyStatus('no-data')
    })
  },
  setEmptyStatus (status) {
    if (status === 'loading') {
      this.setData({
        ['emptyParams.loadingFlag']: true,
        ['emptyParams.loadText']: '加载中...'
      })
    } else if (status === 'no-data') {
      this.setData({
        ['emptyParams.loadingFlag']: false,
        ['emptyParams.loadText']: '暂无数据'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})